#pragma once
#include "vn/HGlobalStream.h"
#include "tests/stub_StreamTest.h"
#include "vn/Mem.h"

void HGlobalStreamTest() {_test_func_;
	{
		HGlobalStream a;
		a.Write("0987654321", 10);
		stub_StreamTest_can(a, true, true, true);
		stub_StreamTest_seek(a);
		stub_StreamTest_read_write(a);
		a.Close();
		stub_StreamTest_exception_if_close(a);
	}

	{
		HGlobalStream a;
			assert(a.GetLength() == 0);
			assert(a.GetPosition() == 0);
			assert(a.CanRead());
			assert(a.CanWrite());
			assert(a.CanSeek());

		a.Write("12345", 5);
			assert(a.GetLength() == 5);
			assert(a.GetPosition() == 5);
			assert(a.CanRead());
			assert(a.CanWrite());
			assert(a.CanSeek());

		a.SetPosition(10);
			assert(a.GetLength() == 5);
			assert(a.GetPosition() == 10);
		a.Write("12", 2);
			assert(a.GetLength() == 12);
			assert(a.GetPosition() == 12);
			assert(a.CanRead());
			assert(a.CanWrite());
			assert(a.CanSeek());

		a.Seek(4, SeekBegin);
			assert(a.GetPosition() == 4);
		a.Seek(4, SeekCurrent);
			assert(a.GetPosition() == 8);
		a.Seek(-4, SeekEnd);
			assert(a.GetPosition() == a.GetLength() - 4);

		Buf8 buf[100];
		Buf8 exp[] = "345\0\0\0\0\00012";
		a.Seek(2, SeekBegin);
		StmLen r = a.Read(buf, 100);
		assert(a.GetPosition() == 12);
		assert(a.GetLength() == 12);
		assert(r == 10);
		// code err? assert(memcpy_s(buf, 10, exp, 10) == 0);
		assert( Mem::Compare(buf, exp, 10) == 0 );
		r = a.Read(buf, 100);
		assert(r == 0); // read return 0
	}


	{
		HGlobalStream a;
		a.Write("Hello\r\nbOOJOUR", 5);
		a.Write("\r\n", 2);
		a.Write("Bonjour", 7);
		a.Flush();
		a.Close();
	}
}
